package work.vcloud.baseinfo.controller;

import work.vcloud.baseinfo.exception.BaseException;
import work.vcloud.baseinfo.service.impl.HardWareMonitorServiceImpl;
import work.vcloud.baseinfo.service.intfc.HardWareMonitorService;
import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;
import work.vcloud.frame.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONArray;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Scope("prototype")
public class AppController extends BaseController {

	@Autowired
	OpenDao openDao;

	@Resource
	HardWareMonitorService hardWareMonitorService;

	/**
	 * 查询应用信息
	 */
	@RequestMapping("api/app.select")
	public OutputVo select() {
		try {
			Map<String, Object> map = getParameterMap();
			PageVo page = readPage(request);
			map.put("page", page);
			map.put("sqlMapId", "app.select");
			List<Map<String, Object>> applist = openDao.queryForList(map);
			map.remove("page");
			if(applist.size() > 0) {
                for (int i = 0; i < applist.size(); i++) {
                    Map<String, Object> app = applist.get(i);
                    map.put("fk_app",app.get("client_id"));
                    map.put("sqlMapId","app.selectAppUser");
                    List<Map<String,Object>> usersList =openDao.queryForList(map);
                    if(usersList.size() > 0) {
                    	app.put("userTypeList",usersList);
                    }
                    map.put("sqlMapId","app.selectAppType");
                    List<Map<String,Object>> typesList =openDao.queryForList(map);
                    if(typesList.size() > 0) {
                    	app.put("appTypeList",typesList);
                    }
                }
            }
			return output("0000", applist, page);
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	/**
	 * 添加应用信息
	 */
	@RequestMapping("api/app.insert")
	public OutputVo insert() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			map.put("sqlMapId", "app.insert");
			Object fk_app = openDao.insert(map);
			map.put("fk_app", fk_app.toString());
			JSONArray usertype = JSONArray.parseArray(map.get("userType").toString());
			map.put("sqlMapId", "app.insertAppUser");
			for(int i=0;i<usertype.size();i++) {
				map.put("user_type", usertype.get(i));
				openDao.insert(map);
			}
			
			JSONArray apptype = JSONArray.parseArray(map.get("appType").toString());
			map.put("sqlMapId", "app.insertAppType");
			for(int i=0;i<apptype.size();i++) {
				map.put("app_type", apptype.get(i));
				openDao.insert(map);
			}
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	
	/**
	 * 修改查询应用信息
	 */
	@RequestMapping("api/app.update")
	public OutputVo update() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			map.put("sqlMapId", "app.update");
			openDao.update(map);
			map.put("fk_app",map.get("client_id"));
			map.put("sqlMapId", "app.deleteAppUser");
			openDao.update(map);
			JSONArray usertype = JSONArray.parseArray(map.get("userType").toString());
			map.put("sqlMapId", "app.insertAppUser");
			for(int i=0;i<usertype.size();i++) {
				map.put("user_type", usertype.get(i));
				openDao.insert(map);
			}
			
			map.put("sqlMapId", "app.deleteAppType");
			openDao.update(map);
			JSONArray apptype = JSONArray.parseArray(map.get("appType").toString());
			map.put("sqlMapId", "app.insertAppType");
			for(int i=0;i<apptype.size();i++) {
				map.put("app_type", apptype.get(i));
				openDao.insert(map);
			}
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	
	/**
	 * 添加应用启用单位信息
	 */
	@RequestMapping("api/app.insertAppUnit")
	public OutputVo insertAppUnit() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			JSONArray units = JSONArray.parseArray(map.get("units").toString());
			if(units.size() > 0) {
                map.put("sqlMapId", "app.deleteAppUnit");
                openDao.delete(map);
                
				for(int i = 0;i<units.size();i++) {
                    map.put("fk_unit",units.get(i));
                    map.put("sqlMapId", "app.insertAppUnit");
                    openDao.insert(map);
				}
			}
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}

	/**
	 * 获取资源占用率
	 *
	 * @return
	 */
	@RequestMapping("clientApi/monitor/getResourceRate")
	public Object getResourceRate() {
		try {
			Map map = getParameterMap();
			map.remove("page");

			SimpleDateFormat sdf = new SimpleDateFormat("mm:ss:SSS");

			int lastCount = 5;
			List resultList = new ArrayList();
			HardWareMonitorServiceImpl.TimeRate[] cpuRateArray = hardWareMonitorService.getCPURateArray(lastCount);
			HardWareMonitorServiceImpl.TimeRate[] memoryRateArray = hardWareMonitorService.getMemoryRateArray(lastCount);

			for (int i = 0; i < lastCount; i++) {
				HardWareMonitorServiceImpl.TimeRate cpuTimeRate = cpuRateArray[i];
				HardWareMonitorServiceImpl.TimeRate memoryTimeRate = memoryRateArray[i];
				Map tMap = new HashMap();
				tMap.put("time", sdf.format(cpuTimeRate.getDate()));
				tMap.put("cpu", cpuTimeRate.getRate() * 100);
				tMap.put("memory", memoryTimeRate.getRate() * 100);
				tMap.put("resource", (cpuTimeRate.getRate() + memoryTimeRate.getRate()) / 2 * 100);
				resultList.add(tMap);
			}

			return resultList;
		} catch (BaseException e) {
			e.printStackTrace();
			return output("9999", e.getMessage(), e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "错误", e.getMessage());
		}
	}
}
